#include <gtk/gtk.h>
#include <time.h>
//更新时间
gboolean update_time_label(GtkLabel *label);
//处理信息关闭界面
void handle_info_close(GtkButton *button, gpointer user_data) ;
//处理信息界面
void handle_info(GtkButton *button, gpointer user_data) ;
//处理数据界面
void handle_data(GtkButton *button, gpointer user_data);
//处理数据开启
void on_switch_toggled(GtkSwitch *switch_btn, gpointer user_data);
//退出数据
void off_switch_toggled(GtkSwitch *switch_btn, gpointer user_data);

// 回调函数，用于更新时间
gboolean update_time_label(GtkLabel *label) {
    time_t rawtime;
    struct tm *timeinfo;
    char buffer[80];

    time(&rawtime);
    timeinfo = localtime(&rawtime);

    strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", timeinfo);
    gtk_label_set_text(label, buffer);

    return G_SOURCE_CONTINUE;
}
void handle_info(GtkButton *button, gpointer user_data) {
    GtkBuilder *builder = user_data;  // 获取 GtkBuilder 对象

    // 从 GtkBuilder 获取新窗口
    GtkWidget *new_window = GTK_WIDGET(gtk_builder_get_object(builder, "info_app"));
    gtk_window_set_decorated(GTK_WINDOW(new_window), FALSE);

    GtkButton* close= GTK_BUTTON(gtk_builder_get_object(builder, "close"));

    g_signal_connect(close, "clicked", G_CALLBACK(handle_info_close), new_window);
    // 显示新窗口
    gtk_widget_show_all(new_window);
}
void handle_info_close(GtkButton *button, gpointer user_data) {
    GtkWidget *new_window = user_data;  // 获取 GtkBuilder 对象
    gtk_widget_hide(new_window);
    // 从 GtkBuilder 获取新窗口
   
}
void handle_data(GtkButton *button, gpointer user_data) {
    GtkBuilder *builder = user_data;  // 获取 GtkBuilder 对象
    // 从 GtkBuilder 获取新窗口
    GtkWidget *new_window = GTK_WIDGET(gtk_builder_get_object(builder, "data_app"));
    gtk_window_set_decorated(GTK_WINDOW(new_window), FALSE);

    GtkLabel *piece = GTK_LABEL(gtk_builder_get_object(builder, "Piece"));
    GtkWidget *piece_widget = GTK_WIDGET(piece);
    GtkEntry *piece1 = GTK_ENTRY(gtk_builder_get_object(builder, "piece1"));
    GtkWidget *piece1_widget = GTK_WIDGET(piece1);
  
    GtkWidget *Line1 = GTK_WIDGET(gtk_builder_get_object(builder, "Line1"));
    GtkWidget *Line1_widget = GTK_WIDGET(Line1);
    GtkEntry  *line1 = GTK_ENTRY(gtk_builder_get_object(builder, "line1"));
    GtkWidget *line1_widget = GTK_WIDGET(line1);
    GtkWidget *Line2 = GTK_WIDGET(gtk_builder_get_object(builder, "Line2"));
    GtkWidget *Line2_widget = GTK_WIDGET(Line2);
    GtkEntry  *line2 = GTK_ENTRY(gtk_builder_get_object(builder, "line2"));
    GtkWidget *line2_widget = GTK_WIDGET(line2);
    GtkWidget *Line3 = GTK_WIDGET(gtk_builder_get_object(builder, "Line3"));
    GtkWidget *Line3_widget = GTK_WIDGET(Line3);
    GtkEntry  *line3 = GTK_ENTRY(gtk_builder_get_object(builder, "line3"));
    GtkWidget *line3_widget = GTK_WIDGET(line3);

    GtkSwitch *switch_on = GTK_SWITCH(gtk_builder_get_object(builder, "switch_on"));
    GtkSwitch *switch_off = GTK_SWITCH(gtk_builder_get_object(builder, "switch_off"));
   

   

  
    // 显示新窗口
    gtk_widget_show_all(new_window);
    gtk_widget_hide(piece_widget);
    gtk_widget_hide(piece1_widget);
    gtk_widget_hide(Line1_widget);
    gtk_widget_hide(line1_widget);
    gtk_widget_hide(Line2_widget);
    gtk_widget_hide(line2_widget);
    gtk_widget_hide(Line3_widget);
    gtk_widget_hide(line3_widget);
     g_signal_connect(switch_on, "notify::active", G_CALLBACK(on_switch_toggled), builder);
    g_signal_connect(switch_off, "notify::active", G_CALLBACK(off_switch_toggled), builder);
}
void on_switch_toggled(GtkSwitch *switch_btn, gpointer user_data) {
    GtkBuilder *builder = user_data;
    GtkLabel *piece = GTK_LABEL(gtk_builder_get_object(builder, "Piece"));
    GtkWidget *piece_widget = GTK_WIDGET(piece);
    GtkEntry *piece1 = GTK_ENTRY(gtk_builder_get_object(builder, "piece1"));
    GtkWidget *piece1_widget = GTK_WIDGET(piece1);
  
    GtkWidget *Line1 = GTK_WIDGET(gtk_builder_get_object(builder, "Line1"));
    GtkWidget *Line1_widget = GTK_WIDGET(Line1);
    GtkEntry  *line1 = GTK_ENTRY(gtk_builder_get_object(builder, "line1"));
    GtkWidget *line1_widget = GTK_WIDGET(line1);
    GtkWidget *Line2 = GTK_WIDGET(gtk_builder_get_object(builder, "Line2"));
    GtkWidget *Line2_widget = GTK_WIDGET(Line2);
    GtkEntry  *line2 = GTK_ENTRY(gtk_builder_get_object(builder, "line2"));
    GtkWidget *line2_widget = GTK_WIDGET(line2);
    GtkWidget *Line3 = GTK_WIDGET(gtk_builder_get_object(builder, "Line3"));
    GtkWidget *Line3_widget = GTK_WIDGET(Line3);
    GtkEntry  *line3 = GTK_ENTRY(gtk_builder_get_object(builder, "line3"));
    GtkWidget *line3_widget = GTK_WIDGET(line3);

    // if (gtk_switch_get_active(switch_btn)) {
    //     // Switch按钮打开时的操作
    // gtk_widget_show(piece_widget);
    // gtk_widget_show(piece1_widget);
    // gtk_widget_show(Line1_widget);
    // gtk_widget_show(line1_widget);
    // gtk_widget_show(Line2_widget);
    // gtk_widget_show(line2_widget);
    // gtk_widget_show(Line3_widget);
    // gtk_widget_show(line3_widget);
    // }
}

void off_switch_toggled(GtkSwitch *switch_btn, gpointer user_data) {
    GtkBuilder *builder = user_data;
   GtkLabel *piece = GTK_LABEL(gtk_builder_get_object(builder, "Piece"));
    GtkWidget *piece_widget = GTK_WIDGET(piece);
    GtkEntry *piece1 = GTK_ENTRY(gtk_builder_get_object(builder, "piece1"));
    GtkWidget *piece1_widget = GTK_WIDGET(piece1);
  
    GtkWidget *Line1 = GTK_WIDGET(gtk_builder_get_object(builder, "Line1"));
    GtkWidget *Line1_widget = GTK_WIDGET(Line1);
    GtkEntry  *line1 = GTK_ENTRY(gtk_builder_get_object(builder, "line1"));
    GtkWidget *line1_widget = GTK_WIDGET(line1);
    GtkWidget *Line2 = GTK_WIDGET(gtk_builder_get_object(builder, "Line2"));
    GtkWidget *Line2_widget = GTK_WIDGET(Line2);
    GtkEntry  *line2 = GTK_ENTRY(gtk_builder_get_object(builder, "line2"));
    GtkWidget *line2_widget = GTK_WIDGET(line2);
    GtkWidget *Line3 = GTK_WIDGET(gtk_builder_get_object(builder, "Line3"));
    GtkWidget *Line3_widget = GTK_WIDGET(Line3);
    GtkEntry  *line3 = GTK_ENTRY(gtk_builder_get_object(builder, "line3"));
    GtkWidget *line3_widget = GTK_WIDGET(line3);

    // if (!gtk_switch_get_active(switch_btn)) {
    //     // Switch按钮关闭时的操作
    // gtk_widget_hide(piece_widget);
    // gtk_widget_hide(piece1_widget);
    // gtk_widget_hide(Line1_widget);
    // gtk_widget_hide(line1_widget);
    // gtk_widget_hide(Line2_widget);
    // gtk_widget_hide(line2_widget);
    // gtk_widget_hide(Line3_widget);
    // gtk_widget_hide(line3_widget);
    // }
}
int main(int argc, char *argv[]) {
    // 初始化 GTK
    gtk_init(&argc, &argv);
    
    // 创建 GtkBuilder 对象
    GtkBuilder *builder = gtk_builder_new();
    
    // 读取 Glade XML 文件
    gtk_builder_add_from_file(builder, "tg02.test", NULL);

    // 从 GtkBuilder 获取窗口
    GtkWidget *main_app = GTK_WIDGET(gtk_builder_get_object(builder, "main_app"));
   //隐藏标题栏
    gtk_window_set_decorated(GTK_WINDOW(main_app), FALSE);
    //实时标签
    GtkLabel *time_label = GTK_LABEL(gtk_builder_get_object(builder, "timelabel"));
    //设置gtknotebook
    GtkNotebook * notebook1 = GTK_NOTEBOOK(gtk_builder_get_object(builder, "notebook1"));;

    GtkButton* info = GTK_BUTTON(gtk_builder_get_object(builder, "info"));

    g_signal_connect(info, "clicked", G_CALLBACK(handle_info), builder);

    GtkButton* data = GTK_BUTTON(gtk_builder_get_object(builder, "Data"));


    g_signal_connect(data, "clicked", G_CALLBACK(handle_data), builder);



    // 设置主窗口关闭时的操作
    g_signal_connect(main_app, "destroy", G_CALLBACK(gtk_main_quit), NULL);
     // 使用 g_timeout_add 设置每秒更新一次时间的定时器
    g_timeout_add_seconds(1, (GSourceFunc)update_time_label, time_label);
    // 显示主窗口
    gtk_widget_show_all(main_app);

    // 运行 GTK 主循环
    gtk_main();

    return 0;
}
